package com.pro.system.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.pro.system.auto.entity.User;
import com.pro.system.auto.entity.UserCriteria;
import com.pro.system.auto.mapper.UserMapper;
import com.pro.system.exception.ServiceException;
import com.pro.system.service.UserService;
import com.pro.system.utils.Page;

@Transactional
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User loadUserByUsername(String username) throws ServiceException{
		UserCriteria userCriteria =  new UserCriteria();
		userCriteria.createCriteria().andUserNameEqualTo(username);
		List<User> userList = userMapper.selectByExample(userCriteria);
		if (!CollectionUtils.isEmpty(userList)) {
			return userList.get(0);
		} else {
			throw new ServiceException("not found user");
		}
	}

	@Override
	public Page<User> findUserList(Integer pageSize, Integer pageNum,String userName) {
		Page<User> page = new Page<User>((pageNum-1) *pageSize, pageSize);
		UserCriteria userCriteria = new UserCriteria();
		
		if (StringUtils.isNotBlank(userName)) {
			userCriteria.createCriteria().andUserNameLike("%" + userName + "%");
		}else {
			userCriteria.createCriteria();
		}
		int count = userMapper.countByExample(userCriteria);
		userCriteria.setPage(page);
		List<User> users = userMapper.selectByExample(userCriteria);
		page.setResult(users);
		page.setTotalRecords(count);
		return page;
	}
	
	/***
	 * 检查用户是否存在
	 */
	public boolean checkUser(String userName) {
		
		UserCriteria userCriteria = new UserCriteria();
		userCriteria.createCriteria().andUserNameEqualTo(userName);
		List<User> userList = this.userMapper.selectByExample(userCriteria);
		if (!CollectionUtils.isEmpty(userList)) {
			return true;
		}
		return false;
	}
	
	/***
	 * 检查邮箱是否存在
	 */
	public boolean checkEmail(String email) {
		
		UserCriteria userCriteria = new UserCriteria();
		userCriteria.createCriteria().andEmailEqualTo(email);
		List<User> userList = this.userMapper.selectByExample(userCriteria);
		if (!CollectionUtils.isEmpty(userList)) {
			return true;
		}
		return false;
	}

	/***
	 * 检查昵称是否存在
	 */
	public boolean checkNickName(String nickName) {
		
		UserCriteria userCriteria = new UserCriteria();
		userCriteria.createCriteria().andNickNameEqualTo(nickName);
		List<User> userList = this.userMapper.selectByExample(userCriteria);
		if (!CollectionUtils.isEmpty(userList)) {
			return true;
		}
		return false;
	}

	/***
	 * 检查邮箱是否被占用
	 */
	public boolean checkMobile(String mobile) {

		UserCriteria userCriteria = new UserCriteria();
		userCriteria.createCriteria().andMobileEqualTo(mobile);
		List<User> userList = this.userMapper.selectByExample(userCriteria);
		if (!CollectionUtils.isEmpty(userList)) {
			return true;
		}
		return false;
	}
	
	/***
	 * 用户注册
	 */
	public void regUser(User user) {
		user.setState(0);
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setAge(user.getAge() == null ?0:user.getAge());
		this.userMapper.insert(user);
	}

}
